package com.taobaobao.com.taobaobao.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taobaobao.com.taobaobao.R;

public class AddAndDeleteView extends LinearLayout {

    private TextView deleteTv, addTv;
    private EditText numEt;

    public AddAndDeleteView(Context context) {
        super(context);
        init(context);
    }

    public AddAndDeleteView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AddAndDeleteView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_add_and_delete, this);
        deleteTv = findViewById(R.id.delete);
        addTv = findViewById(R.id.add);
        numEt = findViewById(R.id.num_et);
        initAdd();
        initDelete();
    }

    private void initAdd() {
        addTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String numStr = numEt.getText().toString();
                int num = 0;
                if (!TextUtils.isEmpty(numStr)) {
                    num = Integer.valueOf(numStr);
                }
                num++;
                numEt.setText(String.valueOf(num));
            }
        });
    }

    private void initDelete() {
        deleteTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String numStr = numEt.getText().toString();
                int num = 0;
                if (!TextUtils.isEmpty(numStr)) {
                    num = Integer.valueOf(numStr);
                }
                if (0 == num) {
                    num = 0;
                } else {
                    num--;
                }
                numEt.setText(String.valueOf(num));
            }
        });
    }

    public String getNum() {
        return numEt.getText().toString();
    }
}
